package com.ruoyi.video.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.video.server.AbstractServer;
import com.ruoyi.video.task.ServerApplicationRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.video.mapper.VideoServerMapper;
import com.ruoyi.video.domain.VideoServer;
import com.ruoyi.video.service.IVideoServerService;

/**
 * 服务Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-09
 */
@Service
public class VideoServerServiceImpl implements IVideoServerService 
{
    @Autowired
    private VideoServerMapper videoServerMapper;

    /**
     * 查询服务
     * 
     * @param id 服务主键
     * @return 服务
     */
    @Override
    public VideoServer selectVideoServerById(Long id)
    {
        return videoServerMapper.selectVideoServerById(id);
    }

    /**
     * 查询服务列表
     * 
     * @param videoServer 服务
     * @return 服务
     */
    @Override
    public List<VideoServer> selectVideoServerList(VideoServer videoServer)
    {
        return videoServerMapper.selectVideoServerList(videoServer);
    }

    /**
     * 新增服务
     * 
     * @param videoServer 服务
     * @return 结果
     */
    @Override
    public int insertVideoServer(VideoServer videoServer)
    {
        VideoServer old = videoServerMapper.selectVideoServerByCode(videoServer.getCode());
        if(old != null){
            throw new ServiceException("服务代码不能重复");
        }
        videoServer.setCreateTime(new Date());
        return videoServerMapper.insertVideoServer(videoServer);
    }

    /**
     * 修改服务
     * 
     * @param videoServer 服务
     * @return 结果
     */
    @Override
    public int updateVideoServer(VideoServer videoServer)
    {
        VideoServer old = videoServerMapper.selectVideoServerByCode(videoServer.getCode());
        if(old != null && !old.getId().equals(videoServer.getId())){
            throw new ServiceException("服务代码不能重复");
        }
        videoServer.setUpdateTime(new Date());
        return videoServerMapper.updateVideoServer(videoServer);
    }

    /**
     * 批量删除服务
     * 
     * @param ids 需要删除的服务主键
     * @return 结果
     */
    @Override
    public int deleteVideoServerByIds(Long[] ids)
    {
        return videoServerMapper.deleteVideoServerByIds(ids);
    }

    /**
     * 删除服务信息
     * 
     * @param id 服务主键
     * @return 结果
     */
    @Override
    public int deleteVideoServerById(Long id)
    {
        return videoServerMapper.deleteVideoServerById(id);
    }


    /**
     * 启停服务
     *
     * @param code 服务代码
     * @param cmd start/stop
     * @return 结果
     */
    @Override
    public int triggerVideoServer(String code, String cmd){
        VideoServer videoServer = videoServerMapper.selectVideoServerByCode(code);
        if(videoServer == null){
            throw new ServiceException("服务不存在");
        }

        AbstractServer handler = ServerApplicationRunner.getHandler(code);
        if(cmd.equalsIgnoreCase("start")){
            if(handler.alive(videoServer)) {
                return 1;
            }else if(handler.start(videoServer)){
                videoServer.setRunning("1");
                videoServerMapper.updateVideoServer(videoServer);
                return 1;
            }
        }else{
            if(!handler.alive(videoServer)) {
                return 1;
            }else if(handler.stop(videoServer)){
                videoServer.setRunning("0");
                videoServerMapper.updateVideoServer(videoServer);
                return 1;
            }
        }
        return 0;
    }
}
